﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;

namespace TestModBus.Model
{
    public class PerformanceTestModel
    {
        public System.Timers.Timer tmer = null;
        public BussinessModel bussiness = new BussinessModel();
        public PerformanceTestModel()
        {
        }

        public int Ch { get; set; }

        public int ShePinCh { get; set; }

        /// <summary>
        /// 切换通道
        /// </summary>
        /// <param name="ch"></param>
        public void ChangeCh(int ch)
        {
            switch (ch)
            {
                case 1:
                    bussiness.operTypeBt = ModeOperType.CH1;
                    SetRunCh("CH1");
                    break;
                case 2:
                    bussiness.operTypeBt = ModeOperType.CH2;
                    SetRunCh("CH2");
                    break;
                case 3:
                    bussiness.operTypeBt = ModeOperType.CH3;
                    SetRunCh("CH3");
                    break;
                case 4:
                    bussiness.operTypeBt = ModeOperType.CH4;
                    SetRunCh("CH4");
                    break;
                case 5:
                    bussiness.operTypeBt = ModeOperType.CH5;
                    SetRunCh("CH5");
                    break;
                case 6:
                    bussiness.operTypeBt = ModeOperType.CH6;
                    SetRunCh("CH6");
                    break;
                case 7:
                    bussiness.operTypeBt = ModeOperType.CH7;
                    SetRunCh("CH7");
                    break;
                case 8:
                    bussiness.operTypeBt = ModeOperType.CH8;
                    SetRunCh("CH8");
                    break;
                case 9:
                    bussiness.operTypeBt = ModeOperType.CH9;
                    SetRunCh("CH9");
                    break;
                case 10:
                    bussiness.operTypeBt = ModeOperType.CH10;
                    SetRunCh("CH10");
                    break;
                case 11:
                    bussiness.operTypeBt = ModeOperType.CH11;
                    SetRunCh("CH11");
                    break;
                case 12:
                    bussiness.operTypeBt = ModeOperType.CH12;
                    SetRunCh("CH12");
                    break;
            }
        }

        /// <summary>
        /// 切换通道
        /// </summary>
        /// <param name="value">值</param>
        private void SetRunCh(string value)
        {
            bussiness.now_function = value;
            bussiness.SendCh();
        }

        /// <summary>
        /// 切换射频
        /// </summary>
        public void ChangeShePin(int shepin) 
        {
            bussiness.RegisterBuffer[16] = 0;
            bussiness.RegisterBuffer[15] = 0;
            bussiness.RegisterBuffer[14] = 0;
            bussiness.RegisterBuffer[13] = 0;
            bussiness.RegisterBuffer[12] = 0;
            bussiness.RegisterBuffer[11] = 0;
            bussiness.RegisterBuffer[10] = 0;
            bussiness.RegisterBuffer[9] = 0;
            bussiness.RegisterBuffer[8] = 0;
            bussiness.RegisterBuffer[7] = 0;
            bussiness.RegisterBuffer[6] = 0;
            bussiness.RegisterBuffer[5] = 0;
            bussiness.RegisterBuffer[4] = 0;
            bussiness.RegisterBuffer[3] = 0;
            bussiness.RegisterBuffer[2] = 0;
            bussiness.RegisterBuffer[1] = 0;
            bussiness.RegisterBuffer[0] = 0;
            switch (shepin) 
            {
                case 1:     // 信号源-高频/低频-中频输入
                    SetShePinArgs(ModeOperType.Y7);
                    SetShePinArgs(ModeOperType.Y2);
                    break;
                case 2:     // 信号源-合成-中频输入
                    SetShePinArgs(ModeOperType.Y10);
                    SetShePinArgs(ModeOperType.Y2);
                    break;
                case 3:     // 信号源-I/Q-中频输入
                    SetShePinArgs(ModeOperType.Y11);
                    SetShePinArgs(ModeOperType.Y2);
                    break;
                case 4:     // 信号源-备用-中频输入
                    SetShePinArgs(ModeOperType.Y12);
                    SetShePinArgs(ModeOperType.Y2);
                    break;
                case 5:     // 信号源-合成-天线
                    SetShePinArgs(ModeOperType.Y10);
                    SetShePinArgs(ModeOperType.Y2);
                    SetShePinArgs(ModeOperType.Y15);
                    break;
                case 6:     // 信号源-I/Q-天线
                    SetShePinArgs(ModeOperType.Y11);
                    SetShePinArgs(ModeOperType.Y2);
                    SetShePinArgs(ModeOperType.Y15);
                    break;
                case 7:     // 信号源-高频/低频-天线
                    SetShePinArgs(ModeOperType.Y7);
                    SetShePinArgs(ModeOperType.Y2);
                    SetShePinArgs(ModeOperType.Y15);
                    break;
                case 8:     // 信号源-备用-天线
                    SetShePinArgs(ModeOperType.Y12);
                    SetShePinArgs(ModeOperType.Y2);
                    SetShePinArgs(ModeOperType.Y15);
                    break;
                case 9:     // 中频输出-频谱仪
                    //SetShePinArgs(ModeOperType.Y0);
                    SetShePinArgs(ModeOperType.Y1);
                    break;
                case 10:    // 中频输出-噪声仪
                    SetShePinArgs(ModeOperType.Y0);
                    break;
                case 11:    // 噪声仪-天线
                    SetShePinArgs(ModeOperType.Y16);
                    break;
                case 12:    // 中频输出-噪声仪-天线
                    SetShePinArgs(ModeOperType.Y0);
                    SetShePinArgs(ModeOperType.Y16);
                    break;
                case 13:    // 天线-频谱仪
                    SetShePinArgs(ModeOperType.Y20);
                    SetShePinArgs(ModeOperType.Y3);
                    //SetShePinArgs(ModeOperType.Y1);
                    break;
                case 14:    // 天线-矢网
                    SetShePinArgs(ModeOperType.Y17);
                    break;
                case 15:    // 天线-功率计
                    SetShePinArgs(ModeOperType.Y20);
                    SetShePinArgs(ModeOperType.Y4);
                    break;
                case 16:    // 天线-频率计
                    SetShePinArgs(ModeOperType.Y20);
                    SetShePinArgs(ModeOperType.Y5);
                    break;
                case 17:    // 天线-示波器-3
                    SetShePinArgs(ModeOperType.Y20);
                    SetShePinArgs(ModeOperType.Y6);
                    break;
            }
            RunMaxShePinChange();
        }


        /// <summary>
        /// 射频切换操作
        /// </summary>
        /// <param name="operType">操作模式</param>
        private void SetShePinArgs(ModeOperType modeOper)
        {
            bussiness.operTypeBt = modeOper;
            switch (bussiness.operTypeBt)
            {
                case ModeOperType.Y0:
                    if (bussiness.BtY0 == 0)
                    {
                        bussiness.RegisterBuffer[16] = 1;
                        bussiness.BtY0 = 1;
                    }
                    else
                    {
                        bussiness.BtY0 = 0;
                        bussiness.RegisterBuffer[16] = 0;
                    }
                    break;
                case ModeOperType.Y1:
                    if (bussiness.BtY1 == 0)
                    {
                        bussiness.BtY1 = 1;
                        bussiness.RegisterBuffer[15] = 1;
                    }
                    else
                    {
                        bussiness.BtY1 = 0;
                        bussiness.RegisterBuffer[15] = 0;
                    }

                    break;
                case ModeOperType.Y2:
                    if (bussiness.BtY2 == 0)
                    {
                        bussiness.BtY2 = 1;
                        bussiness.RegisterBuffer[14] = 1;
                    }
                    else
                    {
                        bussiness.BtY2 = 0;
                        bussiness.RegisterBuffer[14] = 0;
                    }
                    break;
                case ModeOperType.Y3:
                    if (bussiness.BtY3 == 0)
                    {
                        bussiness.BtY3 = 1;
                        bussiness.RegisterBuffer[13] = 1;
                    }
                    else
                    {
                        bussiness.BtY3 = 0;
                        bussiness.RegisterBuffer[13] = 0;
                    }
                    break;
                case ModeOperType.Y4:
                    if (bussiness.BtY4 == 0)
                    {
                        bussiness.BtY4 = 1;
                        bussiness.RegisterBuffer[12] = 1;
                    }
                    else
                    {
                        bussiness.BtY4 = 0;
                        bussiness.RegisterBuffer[12] = 0;
                    }
                    break;
                case ModeOperType.Y5:
                    if (bussiness.BtY5 == 0)
                    {
                        bussiness.BtY5 = 1;
                        bussiness.RegisterBuffer[11] = 1;
                    }
                    else
                    {
                        bussiness.BtY5 = 0;
                        bussiness.RegisterBuffer[11] = 0;
                    }
                    break;
                case ModeOperType.Y6:
                    if (bussiness.BtY6 == 0)
                    {
                        bussiness.BtY6 = 1;
                        bussiness.RegisterBuffer[10] = 1;
                    }
                    else
                    {
                        bussiness.BtY6 = 0;
                        bussiness.RegisterBuffer[10] = 0;
                    }
                    break;
                case ModeOperType.Y7:
                    if (bussiness.BtY7 == 0)
                    {
                        bussiness.BtY7 = 1;
                        bussiness.RegisterBuffer[9] = 1;
                    }
                    else
                    {
                        bussiness.BtY7 = 0;
                        bussiness.RegisterBuffer[9] = 0;
                    }
                    break;
                case ModeOperType.Y10:
                    if (bussiness.BtY10 == 0)
                    {
                        bussiness.BtY10 = 1;
                        bussiness.RegisterBuffer[8] = 1;
                    }
                    else
                    {
                        bussiness.BtY10 = 0;
                        bussiness.RegisterBuffer[8] = 0;
                    }
                    break;
                case ModeOperType.Y11:
                    if (bussiness.BtY11 == 0)
                    {
                        bussiness.BtY11 = 1;
                        bussiness.RegisterBuffer[7] = 1;
                    }
                    else
                    {
                        bussiness.BtY11 = 0;
                        bussiness.RegisterBuffer[7] = 0;
                    }
                    break;
                case ModeOperType.Y12:
                    if (bussiness.BtY12 == 0)
                    {
                        bussiness.BtY12 = 1;
                        bussiness.RegisterBuffer[6] = 1;
                    }
                    else
                    {
                        bussiness.BtY12 = 0;
                        bussiness.RegisterBuffer[6] = 0;
                    }
                    break;
                case ModeOperType.Y13:
                    if (bussiness.BtY13 == 0)
                    {
                        bussiness.BtY13 = 1;
                        bussiness.RegisterBuffer[5] = 1;
                    }
                    else
                    {
                        bussiness.BtY13 = 0;
                        bussiness.RegisterBuffer[5] = 0;
                    }
                    break;
                case ModeOperType.Y14:
                    if (bussiness.BtY14 == 0)
                    {
                        bussiness.BtY14 = 1;
                        bussiness.RegisterBuffer[4] = 1;
                    }
                    else
                    {
                        bussiness.BtY14 = 0;
                        bussiness.RegisterBuffer[4] = 0;
                    }
                    break;
                case ModeOperType.Y15:
                    if (bussiness.BtY15 == 0)
                    {
                        bussiness.BtY15 = 1;
                        bussiness.RegisterBuffer[3] = 1;
                    }
                    else
                    {
                        bussiness.BtY15 = 0;
                        bussiness.RegisterBuffer[3] = 0;
                    }
                    break;
                case ModeOperType.Y16:
                    if (bussiness.BtY16 == 0)
                    {
                        bussiness.BtY16 = 1;
                        bussiness.RegisterBuffer[2] = 1;
                    }
                    else
                    {
                        bussiness.BtY16 = 0;
                        bussiness.RegisterBuffer[2] = 0;
                    }
                    break;
                case ModeOperType.Y17:
                    if (bussiness.BtY17 == 0)
                    {
                        bussiness.BtY17 = 1;
                        bussiness.RegisterBuffer[1] = 1;
                    }
                    else
                    {
                        bussiness.BtY17 = 0;
                        bussiness.RegisterBuffer[1] = 0;
                    }
                    break;
                case ModeOperType.Y20:
                    if (bussiness.BtY20 == 0)
                    {
                        bussiness.BtY20 = 1;
                        bussiness.RegisterBuffer[0] = 1;
                    }
                    else
                    {
                        bussiness.BtY20 = 0;
                        bussiness.RegisterBuffer[0] = 0;
                    }
                    break;
            }
        }

        /// <summary>
        /// 执行射频矩阵切换
        /// </summary>
        private void RunMaxShePinChange()
        {
            bussiness.SendShePin();
        }


        /// <summary>
        /// 颜色定义
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public Color SetRunColorState(ColorType state)
        {
            Color color = Color.Transparent;
            switch (state)
            {
                case ColorType.Default:
                    color = Color.DarkGray;
                    break;
                case ColorType.Error:
                    color = Color.Red;
                    break;
                case ColorType.Success:
                    color = Color.Green;
                    break;
            }
            return color;
        }
    }
}
